package com.such.kit.database.bean;

import java.util.HashMap;
import java.util.Map;
import com.such.kit.string.StringCommons;
import com.such.kit.validate.ValidateCommons;

public class MigrationCompareResult {

	/** STRATEGY_UNIT_DEFINITION_CREATE：创建表（表不存在） */
	public static final String STRATEGY_UNIT_DEFINITION_CREATE = "Create";
	/** STRATEGY_UNIT_DEFINITION_RECREATE：（删除后）重建表 */
	public static final String STRATEGY_UNIT_DEFINITION_RECREATE = "Recreate";
	/** STRATEGY_UNIT_DEFINITION_KEEP：保留表结构 */
	public static final String STRATEGY_UNIT_DEFINITION_KEEP = "Keep";
	/** STRATEGY_UNIT_DATA_CREATE：（保留原数据并）导入数据 */
	public static final String STRATEGY_UNIT_DATA_CREATE = "Import";
	/** STRATEGY_UNIT_DATA_RECREATE：（删除原数据后）导入数据 */
	public static final String STRATEGY_UNIT_DATA_RECREATE = "Reimport";

	/** STRATEGY_CREATE_IMPORT：创建表（表不存在）并（保留原数据并）导入数据 */
	public static final String STRATEGY_CREATE_IMPORT = StringCommons.merger(STRATEGY_UNIT_DEFINITION_CREATE, STRATEGY_UNIT_DATA_CREATE);
	/** STRATEGY_RECREATE_IMPORT：（删除后）重建表并（保留原数据并）导入数据 */
	public static final String STRATEGY_RECREATE_IMPORT = StringCommons.merger(STRATEGY_UNIT_DEFINITION_RECREATE, STRATEGY_UNIT_DATA_CREATE);
	/** STRATEGY_KEEP_REIMPORT：保留表结构并（删除原数据后）重新导入数据 */
	public static final String STRATEGY_KEEP_REIMPORT = StringCommons.merger(STRATEGY_UNIT_DEFINITION_KEEP, STRATEGY_UNIT_DATA_RECREATE);

	private Map<DDLTableKey, String> strategyMap = new HashMap<DDLTableKey, String>();

	/**
	 * <p>方法名：addStrategy</p>
	 * <p>描述：为 tableKey 指定策略</p>
	 * @param tableKey
	 * @param strategy 策略
	 */
	public void addStrategy(DDLTableKey tableKey, String strategy) {
		this.strategyMap.put(tableKey, strategy);
	}

	/**
	 * <p>方法名：existTable</p>
	 * <p>描述：根据为 tableKey 指定的同步策略检查表是否存在</p>
	 * @param tableKey
	 * @return boolean true 为存在
	 */
	public boolean existTable(DDLTableKey tableKey) {
		String strategy = this.strategyMap.get(tableKey);
		if (ValidateCommons.isEmpty(strategy)) {
			return false;
		}
		return strategy.startsWith(STRATEGY_UNIT_DEFINITION_RECREATE) || strategy.startsWith(STRATEGY_UNIT_DEFINITION_KEEP);
	}

	/**
	 * <p>方法名：dropTable</p>
	 * <p>描述：根据为 tableKey 指定的同步策略检查是否需要删除表</p>
	 * @param tableKey
	 * @return boolean true 为删除
	 */
	public boolean dropTable(DDLTableKey tableKey) {
		String strategy = this.strategyMap.get(tableKey);
		if (ValidateCommons.isEmpty(strategy)) {
			return false;
		}
		return strategy.startsWith(STRATEGY_UNIT_DEFINITION_RECREATE);
	}

	/**
	 * <p>方法名：createTable</p>
	 * <p>描述：根据为 tableKey 指定的同步策略检查是否需要创建表</p>
	 * @param tableKey
	 * @return boolean true 为创建
	 */
	public boolean createTable(DDLTableKey tableKey) {
		String strategy = this.strategyMap.get(tableKey);
		if (ValidateCommons.isEmpty(strategy)) {
			return false;
		}
		return strategy.startsWith(STRATEGY_UNIT_DEFINITION_CREATE) || strategy.startsWith(STRATEGY_UNIT_DEFINITION_RECREATE);
	}

	/**
	 * <p>方法名：clearData</p>
	 * <p>描述：根据为 tableKey 指定的同步策略检查是否需要清理数据</p>
	 * @param tableKey
	 * @return boolean true 为清理
	 */
	public boolean clearData(DDLTableKey tableKey) {
		String strategy = this.strategyMap.get(tableKey);
		if (ValidateCommons.isEmpty(strategy)) {
			return false;
		}
		return strategy.endsWith(STRATEGY_UNIT_DATA_RECREATE);
	}

}
